// pages/exercise/exercise.js
import utils from "../../utils/util"
const MIN_P_NUM = 5
const MAX_P_NUM = 50
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        numOfProblemList: [],
        indexOfProblemNumPicker: 0,
        labelList: [{
            name: "标签1",
            value: 0
        }, {
            name: "标签2",
            value: 1
        }]
    },
    tagSearchHandler(e) {
        let key = e.detail.value.toLowerCase();
        let list = this.data.labelList;
        for (let i = 0; i < list.length; i++) {
            let a = key;
            let b = list[i].name.toLowerCase();
            if (b.search(a) != -1) {
                list[i].isShow = true
            } else {
                list[i].isShow = false
            }
        }
        this.setData({
            labelList: list
        })
    },
    navBack() {
        if (getCurrentPages().length > 1) {
            wx.navigateBack({
                delta: 1,
            })
        } else {
            wx.switchTab({
                url: '/pages/exercise/leaderboard/leaderboard',
            })
        }
    },

    startExercise(e) {
        let that = this
        let labelList = this.data.labelList
        let tmpArr = []
        labelList.forEach(element => {
            if (element.checked) {
                tmpArr.push(element.name)
            }
        });
        let tag = tmpArr.join(' ')
        if (tag) {
            wx.showLoading({
                title: '正在获取试卷',
            })
            utils.request({
                url: "/api/user/exercise/collect",
                method: "GET",
                data: {
                    tag: tag,
                    amount: that.data.numOfProblemList[that.data.indexOfProblemNumPicker]
                }
            }).then(function (res) {
                wx.hideLoading()
                if (res.data.ret != 0 || res.data.total == 0) {
                    wx.showToast({
                        title: '获取试卷失败',
                        icon: "error"
                    })
                } else {
                    let exercisePaper = {}
                    exercisePaper["total"] = res.data.total
                    exercisePaper["problems"] = res.data.problems
                    that.setStorageAndNav2Ptoblem(exercisePaper, tag)
                }
            }).catch(function (e) {
                wx.hideLoading()
            }).finally(function () {

            })
        } else {
            wx.showToast({
                title: '请正确选择标签',
                icon: "error"
            })
        }
    },

    setStorageAndNav2Ptoblem(exercisePaper, tag) {
        let tagToShow = tag.split(' ').join(',')
        wx.setStorageSync('exerciseTag', tagToShow)
        app.globalData.problemLunchData = {
            type: "exercise",
            problemnum: 1,
            paper: exercisePaper
        }
        wx.navigateTo({
            url: '/pages/problem/problem',
        })
    },

    pickerChanger(e) {
        this.setData({
            indexOfProblemNumPicker: e.detail.value
        })
    },

    checkboxChange(e) {
        let detail = e.currentTarget.dataset
        let labelList = this.data.labelList
        let index = detail.index
        this.setData({
            [`labelList[${index}].checked`]: !labelList[detail.index].checked
        })
    },

    setLabels() {
        let labelList = []
        let that = this
        wx.showLoading({
            title: '获取标签列表',
        })
        utils.request({
            url: "/api/general/tag/list",
            method: "GET",
            data: ""
        }).then(function (ress) {
            wx.hideLoading()
            if (ress.data.ret != 0) {
                wx.showToast({
                    title: ress.data.msg,
                    icon: "error"
                })
                return
            }
            let tags = ress.data.tags
            let i = 0
            tags.forEach(element => {
                labelList.push({
                    name: element,
                    value: i,
                    isShow: true,
                    checked: false,
                    color: utils.getRandomColor(true)
                })
                i++
            });
            that.setData({
                labelList: labelList
            })
        }).catch(function (e) {
            wx.hideLoading()
        }).finally(function () {})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setStorageSync('tag', null)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        utils.initGlobalData(this)
        let list = []
        for (let i = MIN_P_NUM; i <= MAX_P_NUM; i += 5) {
            list.push(i)
        }
        this.setData({
            numOfProblemList: list
        })
        this.setLabels()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        utils.resetGlobalData()
        wx.setStorageSync('tag', null)
        this.setLabels()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setLabels()
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})